package org.oxenframework.mybatis.generator.config;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "oxen.mybatis.sourcegenerator")
public class SourcegeneratorProperties implements InitializingBean {
		
	//字符编码格式
	private String encoding = "utf-8";
	
	// 作者，默认系统用户名
	private String authorName;

	// java源码跟路径
	private String srcPath = "src/main/java";

	//工程根包路径
	private String rootPackage = "";
	
	//工程内模块名
	private String moduleName = "";


	// service 包名
	private String servicePackage;

	// model 包名
	private String modelPackage;

	// mapper包名
	private String mapperPackage;

	// 数据库地址
	private String dbHost;

	// 数据库端口
	private String dbPort = "3306";

	// 数据库名
	private String dbName;

	// 数据库用户名
	private String dbUser;

	// 数据库密码
	private String dbPassword;

	// 生产者是否需要本地service层
	private boolean haveLocalService = true;

	// 是否强制覆盖文件
	private boolean forceCover = false;

	// 远程服务接口后缀
	private String apiPostfix = "Service";

	// 本地服务接口后缀
	private String localServicePostfix = "Manager";
	
	//需要查询数据用到的字段
	private String queryField="";
	
	//需要查询数据List用到的字段
	private String queryListField="";
	
	//更新数据用到的字段
	private String updateField="";
	
	//删除数据用到的字段
	private String removeField = "";
		
	// 分表字段, 空串表示不分表
	private String shardingField = "";
 
	public String getAuthorName() {
		return authorName;
	}

	public void setAuthorName(String authorName) {
		this.authorName = authorName;
	}
 

	public String getSrcPath() {
		return srcPath;
	}

	public void setSrcPath(String srcPath) {
		this.srcPath = srcPath;
	}

	public String getDbHost() {
		return dbHost;
	}

	public void setDbHost(String dbHost) {
		this.dbHost = dbHost;
	}

	public String getDbPort() {
		return dbPort;
	}

	public void setDbPort(String dbPort) {
		this.dbPort = dbPort;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public String getDbUser() {
		return dbUser;
	}

	public void setDbUser(String dbUser) {
		this.dbUser = dbUser;
	}

	public String getDbPassword() {
		return dbPassword;
	}

	public void setDbPassword(String dbPassword) {
		this.dbPassword = dbPassword;
	}

	public boolean isHaveLocalService() {
		return haveLocalService;
	}

	public void setHaveLocalService(boolean haveLocalService) {
		this.haveLocalService = haveLocalService;
	}

	public boolean isForceCover() {
		return forceCover;
	}

	public void setForceCover(boolean forceCover) {
		this.forceCover = forceCover;
	}

	public String getApiPostfix() {
		return apiPostfix;
	}

	public void setApiPostfix(String apiPostfix) {
		this.apiPostfix = apiPostfix;
	}

	public String getRootPackage() {
		return rootPackage;
	}

	public void setRootPackage(String rootPackage) {
		this.rootPackage = rootPackage;
	}

	public String getServicePackage() {
		return servicePackage;
	}

	public void setServicePackage(String servicePackage) {
		this.servicePackage = servicePackage;
	}

	public String getModelPackage() {
		return modelPackage;
	}

	public void setModelPackage(String modelPackage) {
		this.modelPackage = modelPackage;
	}

	public String getMapperPackage() {
		return mapperPackage;
	}

	public void setMapperPackage(String mapperPackage) {
		this.mapperPackage = mapperPackage;
	}

	public String getLocalServicePostfix() {
		return localServicePostfix;
	}

	public void setLocalServicePostfix(String localServicePostfix) {
		this.localServicePostfix = localServicePostfix;
	}
	
	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
 
	public String getQueryField() {
		return queryField;
	}

	public void setQueryField(String queryField) {
		this.queryField = queryField;
	}

	public String getQueryListField() {
		return queryListField;
	}

	public void setQueryListField(String queryListField) {
		this.queryListField = queryListField;
	}
	
	public String getShardingField() {
		return shardingField;
	}

	public void setShardingField(String shardingField) {
		this.shardingField = shardingField;
	}
	
	public String getRemoveField() {
		return removeField;
	}

	public void setRemoveField(String removeField) {
		this.removeField = removeField;
	}
	
	public String getUpdateField() {
		return updateField;
	}

	public void setUpdateField(String updateField) {
		this.updateField = updateField;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		if (authorName == null) {
			authorName = System.getProperty("user.name");
		}
		String headPackage = this.rootPackage + (isEmpty(this.moduleName)?"":"." + this.moduleName);
 
		if (servicePackage == null) {
			servicePackage = headPackage + ".service";
		}
		if (modelPackage == null) {
			modelPackage = headPackage + ".model";
		}
		if (mapperPackage == null) {
			mapperPackage = headPackage + ".mapper" ;
		}
	}	

	private String toHeadUpper(String data) {
		return String.valueOf(Character.toUpperCase(data.charAt(0))) + data.substring(1);
	}
	
	private String toHeadLow(String data) {
		return String.valueOf(Character.toLowerCase(data.charAt(0))) + data.substring(1);
	}
	
	private  boolean isEmpty(String str) {
		return ((str == null) || (str.length() == 0));
	}
}
